import { TENANT_ID_HEADER } from '@/constants/custom';
import { request } from '../request';

/**
 * Login
 *
 * @param params
 */
export function fetchLoginByPassword(params: Api.Auth.LoginModal) {
  return request<Api.Auth.LoginToken>({
    url: '/rbac/auth/login',
    method: 'post',
    data: params,
    headers: {
      [TENANT_ID_HEADER]: params.tenantId
    }
  });
}

/** Get user info */
export function fetchGetUserInfo() {
  return request<Api.Auth.UserInfo>({ url: '/rbac/auth/getUserInfo' });
}

/**
 * Refresh token
 *
 * @param refreshToken Refresh token
 */
export function fetchRefreshToken(refreshToken: Api.Auth.LoginToken) {
  return request<Api.Auth.LoginToken>({
    url: '/rbac/auth/refreshToken',
    method: 'post',
    data: {
      ...refreshToken
    }
  });
}

/**
 * return custom backend error
 *
 * @param code error code
 * @param msg error message
 */
export function fetchCustomBackendError(code: string, msg: string) {
  return request({ url: '/rbac/auth/error', params: { code, msg } });
}

/** 获取 短信验证码 */
export function fetchGetCaptcha(phone: string) {
  return request<string>({
    url: '/rbac/auth/sendSmsCode',
    method: 'post',
    data: {
      username: phone
    }
  });
}

/** 重置我的密码 */
export function fetchResetMyPassword(model: any) {
  return request<string>({
    url: '/rbac/auth/resetMyPassword',
    method: 'post',
    data: model,
    headers: {
      [TENANT_ID_HEADER]: model.tenantId
    }
  });
}

/** logout current user */
export function fetchLogout() {
  return request<string>({
    url: '/rbac/auth/logout',
    method: 'post'
  });
}
